<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流式输出测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .input-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select, button {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007cba;
            color: white;
            cursor: pointer;
            font-size: 16px;
            margin-top: 10px;
        }
        button:hover {
            background-color: #005a87;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        #output {
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 15px;
            min-height: 200px;
            max-height: 400px;
            overflow-y: auto;
            margin-top: 20px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .status {
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .status.connecting {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .status.connected {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .status.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .mode-info {
            background-color: #e2e3e5;
            border: 1px solid #d6d8db;
            border-radius: 4px;
            padding: 10px;
            margin: 10px 0;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>流式输出测试</h1>
        
        <div class="input-group">
            <label for="testUrl">测试URL：</label>
            <input type="text" id="testUrl" placeholder="请输入测试URL" value="/simple_stream_test.php">
        </div>
        
        <button id="startBtn">开始测试</button>
        <button id="stopBtn" disabled>停止连接</button>
        
        <div id="status" class="status connecting" style="display: none;">
            状态信息将显示在这里
        </div>
        
        <div id="output"></div>
    </div>

    <script>
        const testUrlInput = document.getElementById('testUrl');
        const startBtn = document.getElementById('startBtn');
        const stopBtn = document.getElementById('stopBtn');
        const statusDiv = document.getElementById('status');
        const outputDiv = document.getElementById('output');
        
        let eventSource = null;
        
        // 更新状态显示
        function updateStatus(message, type = 'connecting') {
            statusDiv.textContent = message;
            statusDiv.className = 'status ' + type;
            statusDiv.style.display = 'block';
        }
        
        // 开始流式测试
        function startStreaming() {
            const testUrl = testUrlInput.value.trim();
            
            if (!testUrl) {
                alert('请输入测试URL');
                return;
            }
            
            // 清空输出
            outputDiv.textContent = '';
            
            // 禁用开始按钮，启用停止按钮
            startBtn.disabled = true;
            stopBtn.disabled = false;
            
            updateStatus('正在连接...', 'connecting');
            
            // 创建EventSource连接
            eventSource = new EventSource(testUrl);
            
            // 连接打开事件
            eventSource.onopen = function(event) {
                updateStatus('连接已建立，正在接收数据...', 'connected');
            };
            
            // 接收内容事件
            eventSource.addEventListener('content', function(event) {
                const data = JSON.parse(event.data);
                outputDiv.textContent += data.content + '\n';
                // 滚动到底部
                outputDiv.scrollTop = outputDiv.scrollHeight;
            });
            
            // 开始事件
            eventSource.addEventListener('start', function(event) {
                const data = JSON.parse(event.data);
                updateStatus(data.content, 'connected');
            });
            
            // 结束事件
            eventSource.addEventListener('end', function(event) {
                const data = JSON.parse(event.data);
                updateStatus('测试结束: ' + data.content, 'connected');
                stopStreaming();
            });
            
            // 错误事件
            eventSource.addEventListener('error', function(event) {
                const data = JSON.parse(event.data);
                updateStatus('错误: ' + data.content, 'error');
                stopStreaming();
            });
            
            // 连接错误
            eventSource.onerror = function(event) {
                updateStatus('连接错误，请检查网络或服务器状态', 'error');
                stopStreaming();
            };
        }
        
        // 停止流式连接
        function stopStreaming() {
            if (eventSource) {
                eventSource.close();
                eventSource = null;
            }
            
            startBtn.disabled = false;
            stopBtn.disabled = true;
        }
        
        // 绑定事件
        startBtn.addEventListener('click', startStreaming);
        stopBtn.addEventListener('click', stopStreaming);
        
        // 页面卸载时关闭连接
        window.addEventListener('beforeunload', function() {
            if (eventSource) {
                eventSource.close();
            }
        });
    </script>
</body>
</html>